package vn.mobiistar.videoapp.fragment;

import java.util.ArrayList;

import vn.mobiistar.videoapp.R;
import vn.mobiistar.videoapp.activity.MainActivity;
import vn.mobiistar.videoapp.loader.BaseResult;
import vn.mobiistar.videoapp.loader.CategoryLoader;
import vn.mobiistar.videoapp.other.Category;
import vn.mobiistar.videoapp.other.Define;
import vn.mobiistar.videoapp.other.VideoPlayer;
import vn.mobiistar.videoapp.ui.EmptyLoadingView;
import vn.mobiistar.videoapp.ui.Refreshable;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;
import com.github.pedrovgs.DraggablePanel;

public class CategoryFragment  extends BaseFragment implements
LoaderCallbacks<BaseResult>, Refreshable{
	protected static final String TAG = "CategoryFragment";
	private View content;
	private PagerSlidingTabStrip tabs;
	private ViewPager pager;
	private MyPagerAdapter adapter;
	private Context mContext;
	protected ArrayList<Category> mData;
	protected boolean mIsReachedBottom  =false;
	public CategoryLoader mLoader;
	public EmptyLoadingView mLoading;
	CategoryVideoListFragment[] mCategoryListFragment; 
	public DraggablePanel    mDragablePanel;

	public CategoryFragment(Context ct){
		mContext = ct;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreateView(inflater, container, savedInstanceState);
		content = inflater.inflate(R.layout.fragment_category, null);

		mLoader = new CategoryLoader(mContext);
		mLoader.setType(Define.SERVICE_GET_ALLCATE);
		mData = new ArrayList<Category>();

		tabs = (PagerSlidingTabStrip) content.findViewById(R.id.fc_tabs);
		pager = (ViewPager)content. findViewById(R.id.fc_pager);
		mLoading = (EmptyLoadingView) content.findViewById(R.id.fc_loading);
		mDragablePanel = (DraggablePanel) content
				.findViewById(R.id.fc_draggable_panel);
		
		mPlayer = new VideoPlayer(mContext,this, mDragablePanel);
		((MainActivity)getActivity()).currentFragment = this;
		return content;
	}
	public void setTabAdapter(){
		adapter = new MyPagerAdapter(getFragmentManager());

		pager.setAdapter(adapter);
		pager.setOffscreenPageLimit(mData.size() -1 );
		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
				.getDisplayMetrics());
		pager.setPageMargin(pageMargin);
		tabs.setViewPager(pager);
	}

	// adapter cho tabhost
	public class MyPagerAdapter extends FragmentStatePagerAdapter {
		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}
		@Override
		public CharSequence getPageTitle(int position) {
			return mData.get(position).getName();
		}
		@Override
		public int getCount() {
			return mData.size();
		}
		@Override
		public Fragment getItem(int position) {
			//return mCategoryListFragment[position];
			return CategoryVideoListFragment.getInstance(mContext, mData, position,mDragablePanel,mPlayer);
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		getLoaderManager().initLoader(Define.SERVICE_GET_ALLCATE, null, this);
		this.getView().setFocusableInTouchMode(true);
		this.getView().setOnKeyListener( new OnKeyListener(){
		    @Override
		    public boolean onKey( View v, int keyCode, KeyEvent event ){
		        if( keyCode == KeyEvent.KEYCODE_BACK ){
		        	Log.i(TAG, " Back pressed ");
		        	if(mPlayer!= null) if(mPlayer.mIsInitiated) if(mPlayer.getDraggablePanel()!=null) if(mPlayer.getDraggablePanel().isMaximized())  mPlayer.getDraggablePanel().minimize();
		            return true;
		        }
		        return false;
		    }
		} );
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public Loader<BaseResult> onCreateLoader(int id, Bundle arg1) {
		mLoader.setProgressNotifiable( mLoading);
		return mLoader;
	}

	@Override
	public void onLoadFinished(Loader<BaseResult> loader,final BaseResult result) {
		// TODO Auto-generated method stub
		final int id = loader.getId();
		((MainActivity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if(id == Define.SERVICE_GET_ALLCATE) if (((CategoryLoader.Result) result).mCategory.size() > 0) {
					mData = ((CategoryLoader.Result) result).mCategory;
					// set adapter de hien thi danh sach cac tab tren thanh tabhost
					setTabAdapter();
				} else
					mIsReachedBottom = true;
			}
		});
	}

	@Override
	public void onLoaderReset(Loader<BaseResult> arg0) {
	}

	@Override
	public void refreshData() {
	}
}
